package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.EcologyPoints;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsDetailsVO;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsPageVO;
import com.sc.nft.mapper.EcologyPointsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：DaoEcologyPointsWalletDao
 * @Date：2024/11/26 11:08
 * @Filename：DaoEcologyPointsWalletDao
 */
@Slf4j
@Repository
public class EcologyPointsDao extends ServiceImpl<EcologyPointsMapper, EcologyPoints> {




    public EcologyPoints getOne(Long id, Long daoId) {
        LambdaQueryWrapper<EcologyPoints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoints::getId, id);
        wrapper.eq(EcologyPoints::getDaoId, daoId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }
    /**
     * 判断名字是否有重复
     * @param pointsName
     * @return
     */
    public Boolean getEcologyPointsByName(String pointsName,Long daoId) {
        LambdaQueryWrapper<EcologyPoints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoints::getDaoId,daoId);
        wrapper.eq(EcologyPoints::getPointsName, pointsName);
        Integer selectCount = baseMapper.selectCount(wrapper);
        return SqlHelper.retBool(selectCount);
    }



    /**
     * 判断除了自身id的name,其他的名字是否有重复
     * @param pointsName
     * @param id
     * @return
     */
    public Boolean getEcologyPointsByNameNotId(String pointsName,Long id) {
        LambdaQueryWrapper<EcologyPoints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoints::getPointsName, pointsName);
        wrapper.eq(EcologyPoints::getIsDelete, Boolean.FALSE);
        wrapper.ne(EcologyPoints::getId, id);
        wrapper.last("limit 1");
        Integer selectCount = baseMapper.selectCount(wrapper);
        return SqlHelper.retBool(selectCount);
    }

    public Boolean updateEcologyPoints(Long id,String pointsName,String pointsImage,Integer sort) {
        LambdaUpdateWrapper<EcologyPoints> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyPoints::getId, id);
        wrapper.set(ObjectUtil.isNotNull(pointsName), EcologyPoints::getPointsName, pointsName);
        wrapper.set(ObjectUtil.isNotNull(pointsImage), EcologyPoints::getPointsImage, pointsImage);
        wrapper.set(ObjectUtil.isNotNull(sort), EcologyPoints::getSort, sort);
        return update(new EcologyPoints(), wrapper);
    }

    /**
     * 积分列表 根据daoId
     * @param daoId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public IPage<AdminEcologyPointsPageVO> adminEcologyPointsPage(Long daoId, Integer pageNo, Integer pageSize) {
        return baseMapper.adminEcologyPointsPage(new Page<>(pageNo,pageSize),daoId);
    }


    public AdminEcologyPointsDetailsVO adminEcologyPointsDetails(Long id) {
        return baseMapper.adminEcologyPointsDetails(id);
    }


}
